package learnjava.basic.other;
import java.util.*;
public class TestCollection {

    public static void main(String[] args) {

        //测试List的特有迭代器
//        ArrayList<Integer> arr = new ArrayList<>();
////        arr.add(1);
////        arr.add(2);
////
////        ListIterator<Integer> li = arr.listIterator();
////        while (li.hasNext()){
////            Integer integer = li.next();
////            li.set(6);//ListIterator支持增删改
////        }
////        ListIterator<Integer> li1 = arr.listIterator();
////        while (li1.hasNext()){
////            Integer integer = li1.next();
////            System.out.println(integer);
////        }

        //测试自定义equal函数的类存入的HashSet
        //HashSet判断元素是否存在时，先看元素的hashcode，再看equal
//        HashSet<Person> hashSet = new HashSet<>();
//        hashSet.add(new Person("zhansan",1));
//        hashSet.add(new Person("lisi",4));
//        hashSet.add(new Person("lisi2",4));
//        System.out.println(hashSet.size());

        //测试自定义了比较函数的类加入TreeSet
//        TreeSet<Person> treeSet = new TreeSet<>();
//        treeSet.add(new Person("zhansan",1));
//        treeSet.add(new Person("lisi",5));
//        treeSet.add(new Person("wangwu",3));
//        Iterator<Person> iterator = treeSet.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next().name);
//        }

        //测试Collection的排序和查找
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(5);
        arrayList.add(4);
        arrayList.add(2);
//        Collections.sort(arrayList);
        System.out.println(arrayList);
//        System.out.println(Collections.binarySearch(arrayList,1));
//        [5, 4, 2]  -1,，二分查找只能对有序list数组查找
        Collections.reverse(arrayList);
        System.out.println(arrayList);
    }
}

class Person implements Comparable{
    String name;
    int age;

    public Person() {}


    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public boolean equals(Object obj) {
        Person person = (Person) obj;
        return this.toString().equals(person.toString());
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Person p = (Person) o;
        if(this.age>p.age){
            return 1;
        }else if(this.age<p.age){
            return -1;
        }else return 0;
    }
}
